<template>
	<view class="container">
		<view class="logo">
			<img class="img" src="@/static/images/logo.jpg" alt="">
		</view>
		<view class="content">
			<view class="login">
				<u-button type="primary" open-type="getUserInfo" @click="toLogin" text="一键授权登录"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		login
	} from '../../utils/api.js'
	export default {
		data() {
			return {

			};
		},
		methods: {
			toLogin() {
				uni.login({
					"provider": "weixin",
					"onlyAuthorize": true, // 微信登录仅请求授权认证
					success: function(event) {
						const {
							code
						} = event
						login({
							code: code
						}).then(res => {
							if (res.code === 200) {
								console.log(res)
								//获得token完成登录
								uni.setStorageSync('token', res.data)
								uni.switchTab({
									url: '/pages/index/index'
								})
								console.log('登录成功')
							}
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.container {
		text-align: center;

		.logo {
			padding-top: 200rpx;

			.img {
				width: 200rpx;
				height: 200rpx;
			}
		}

		.login {
			margin-top: 100rpx;
			padding: 40rpx;
		}
	}
</style>
